//  Abstract:
//    See gopsed-detailwindow.h
//
//  Reference:
//    See gopsed-detailwindow.h
//
//  Design Notes:
//    See gopsed-detailwindow.h
//
//  Disclaimer:
//    Copyright (C) 2008, Janice Law <janicel700@gmail.com>.
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
#include <libglademm.h>
#include <gtkmm.h>
#include <boost/shared_ptr.hpp>
#include <boost/tuple/tuple.hpp>
#include "gopsed-detailwindow.h"
#include "opf.h"

using namespace std;
using namespace boost;
using Glib::ustring;
using Glib::RefPtr;

namespace gopsed
{
DetailWindow::DetailWindow(const RefPtr<Gnome::Glade::Xml>& glade_xml)
{
    glade = glade_xml;
    glade->get_widget("dialog1", dialog);
    glade->get_widget("scrolledwindow3", container);
    liststore = Gtk::ListStore::create(columns);
    treeview = new Gtk::TreeView(liststore);
}

void DetailWindow::on_edited(const ustring& path, const ustring& new_text)
{
    //Gtk::CellRendererText* renderer = dynamic_cast<Gtk::CellRendererText*>(treeview->get_column(0)->get_first_cell_renderer());
    //renderer->signal_edited().connect(sigc::mem_fun(*this, &DetailWindow::on_edited));
    Gtk::TreePath path_obj = Gtk::TreePath(path);
    const Gtk::TreeRow row(*(treeview->get_model()->get_iter(path)));
    row[columns.m_content] = new_text;
}

Epub::Opf::ustring_1_list DetailWindow::show(Epub::Opf::ustring_1_list param)
{
    Epub::Opf::ustring_1_list result = param;
    Gtk::ListStore::iterator row_it;
    Epub::Opf::ustring_1_list::iterator it;
    for (it = result.begin(); it != result.end(); it++)
    {
        row_it = liststore->append();
        (*row_it)[columns.m_content] = (*it).get<0>();
    }
    treeview->append_column("Content", columns.m_content);
    container->add(*treeview);
    container->show_all();
    int result_val = dialog->run();
    dialog->hide();
    if (result_val == Gtk::RESPONSE_OK)
    {
        // TODO: Writeback of data 
    }
    container->remove();
    return result;
}

Epub::Opf::ustring_2_list DetailWindow::show(const ustring attrName1,
        Epub::Opf::ustring_2_list param)
{
    Epub::Opf::ustring_2_list result = param;
    Gtk::ListStore::iterator row_it;
    Epub::Opf::ustring_2_list::iterator it;
    for (it = result.begin(); it != result.end(); it++)
    {
        row_it = liststore->append();
        (*row_it)[columns.m_content] = (*it).get<0>();
        (*row_it)[columns.m_attr1] = (*it).get<1>();
    }
    treeview->append_column("Content", columns.m_content);
    treeview->append_column(attrName1, columns.m_attr1);
    container->add(*treeview);
    container->show_all();
    int result_val = dialog->run();
    dialog->hide();
    if (result_val == Gtk::RESPONSE_OK)
    {
        // TODO: Writeback of data 
    }
    container->remove();
    return result;
}

Epub::Opf::ustring_3_list DetailWindow::show(const ustring attrName1,
        const ustring attrName2, Epub::Opf::ustring_3_list param)
{
    Epub::Opf::ustring_3_list result = param;
    Gtk::ListStore::iterator row_it;
    Epub::Opf::ustring_3_list::iterator it;
    for (it = result.begin(); it != result.end(); it++)
    {
        row_it = liststore->append();
        (*row_it)[columns.m_content] = (*it).get<0>();
        (*row_it)[columns.m_attr1] = (*it).get<1>();
        (*row_it)[columns.m_attr2] = (*it).get<2>();
    }
    treeview->append_column("Content", columns.m_content);
    treeview->append_column(attrName1, columns.m_attr1);
    treeview->append_column(attrName2, columns.m_attr2);
    container->add(*treeview);
    container->show_all();
    int result_val = dialog->run();
    dialog->hide();
    if (result_val == Gtk::RESPONSE_OK)
    {
        // TODO: Writeback of data 
    }
    container->remove();
    return result;
}
} // gopsed
